package com.apache.poi.word.controller;

import com.apache.poi.word.config.policy.TableRenderPolicy;
import com.apache.poi.word.domain.vo.word.table.AgainstVO;
import com.apache.poi.word.utils.WordUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: HUANG GY.
 * @Date: 2022/2/7 14:10
 * @Description: com.apache.poi.word.controller
 */
@RestController
public class TableController {

    @GetMapping("/test001")
    public static Object test001() throws Exception {

        List<AgainstVO> againstList = new ArrayList<>() ;
        for (int i = 1; i <= 6; i++) {
            AgainstVO vo = AgainstVO
                    .builder()
                    .number(i)
                    .organisationNameHost("队伍"+i)
                    .organisationNameGuest("队伍"+(6-i))
                    .date("2022-10-2"+i)
                    .time("09:00")
                    .location(i+"")
                    .againstHost(i+"")
                    .againstGuest((6-i)+"")
                    .build();
            againstList.add(vo);
        }


        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        InputStream resource = WordUtil.getFileInputStream("docs\\table\\table01.docx");

        Configure config = Configure.builder()
                .bind("rowlist", policy).build();

        XWPFTemplate template = XWPFTemplate.compile(resource, config).render(
                new HashMap<String, Object>() {{
                    put("group", "第一组");
                    put("rowlist", againstList);
                }}
        );
        template.writeAndClose(new FileOutputStream("D:\\05_code\\gitee\\word-demo\\src\\main\\resources\\docs\\table\\table001.docx"));

        return "啊哈哈test001";
    }

    @GetMapping("/test002")
    public static Object test002() throws Exception {

        List<AgainstVO> againstList = new ArrayList<>() ;
        for (int i = 1; i <= 4; i++) {
            for (int j = i+1; j <= 4; j++) {
                Integer number = 4*(i-1)+j ;
                AgainstVO vo = AgainstVO
                        .builder()
                        .number(number)
                        .organisationNameHost("队伍" + i)
                        .organisationNameGuest("队伍" + j)
                        .date("2022-10-2" + i)
                        .time("09:00")
                        .location(number+"")
                        .againstHost(i + "")
                        .againstGuest(j + "")
                        .build();
                againstList.add(vo);
            }
        }
        List<Map<String,String>> rowMapList = new ArrayList<>();
        List<String> hostList = againstList.stream().map(AgainstVO::getOrganisationNameHost).distinct().sorted().collect(Collectors.toList());
        List<String> guestList = againstList.stream().map(AgainstVO::getOrganisationNameGuest).distinct().sorted().collect(Collectors.toList());
        hostList.addAll(guestList);
        List<String> organisationList = hostList.stream().distinct().sorted().collect(Collectors.toList());
        String key = "key" ;
        Map<String,String> rowHeadMap = new LinkedHashMap<>();
        rowMapList.add(rowHeadMap);
        int headIndex = 1 ;
        rowHeadMap.put(key+(headIndex++),"单位");
        for (int i = 0; i < organisationList.size(); i++) {
            int index = 1 ;
            String organisationNameA =  organisationList.get(i) ;
            rowHeadMap.put(key+(headIndex++),organisationNameA);
            Map<String,String> rowMap = new LinkedHashMap<>();
            rowMapList.add(rowMap);
            //日期
            rowMap.put(key+(index++),organisationNameA);
            for (int j = 0; j < organisationList.size(); j++) {
                String organisationNameB =  organisationList.get(j) ;
                if(!organisationNameA.equals(organisationNameB)){
                    List<AgainstVO> teamList = againstList.stream().filter(e -> e.getOrganisationNameHost().equals(organisationNameA) && e.getOrganisationNameGuest().equals(organisationNameB)).collect(Collectors.toList());
                    if (teamList.size() > 0) {
                        AgainstVO againstVO = teamList.get(0);
                        String score = againstVO.getAgainstHost() + " : " + againstVO.getAgainstGuest() ;
                        rowMap.put(key+(index++),score);
                    }else{
                        teamList = againstList.stream().filter(e -> e.getOrganisationNameHost().equals(organisationNameB) && e.getOrganisationNameGuest().equals(organisationNameA)).collect(Collectors.toList());
                        if (teamList.size() > 0) {
                            AgainstVO againstVO = teamList.get(0);
                            String score = againstVO.getAgainstGuest() + " : " + againstVO.getAgainstHost() ;
                            rowMap.put(key+(index++),score);
                        }else{
                            System.out.println("what?");
                            rowMap.put(key+(index++),"");
                        }
                    }
                }else{
                    rowMap.put(key+(index++),"");
                }

            }

        }
        rowHeadMap.put(key+(headIndex++),"积分");
        rowHeadMap.put(key+(headIndex++),"名次");

        int index = 0 ;
        for (Map<String, String> map : rowMapList) {
            if (index == 0) {
                index++ ;
                continue;
            }

            int size = map.size();
            map.put("key"+(size+(index++)),"1");
            map.put("key"+(size+(index++)),"2");
        }

        TableRenderPolicy policy = new TableRenderPolicy();
        InputStream resource = WordUtil.getFileInputStream("docs\\table\\table02.docx");

        Configure config = Configure.builder()
                .bind("rowlist", policy).build();

        XWPFTemplate template = XWPFTemplate.compile(resource, config).render(
                new HashMap<String, Object>() {{
                    put("group", "第一组");
                    put("rowlist", rowMapList);
                }}
        );
        template.writeAndClose(new FileOutputStream("D:\\05_code\\gitee\\word-demo\\src\\main\\resources\\docs\\table\\table002.docx"));

        return "啊哈哈test001";
    }

    public static void main(String[] args) throws Exception {
//        test001();
        test002();
    }

}
